package cn.dida.util;

import java.security.GeneralSecurityException;
import java.util.Calendar;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.util.MailSSLSocketFactory;
/**
 * 系统工具类
 * @author weiwei
 *
 */
public class MailUitl {

	public static boolean sendMessageByMail(String title, String msgs, Address[] arg1) {
		try {
			Properties props = new Properties();

			// 开启debug调试
			props.setProperty("mail.debug", "false");
			// 发送服务器需要身份验证
			props.setProperty("mail.smtp.auth", "true");
			// 设置邮件服务器主机名
			props.setProperty("mail.host", "smtp.exmail.qq.com");

			props.setProperty("mail.smtp.port", "465");
			// 发送邮件协议名称
			props.setProperty("mail.transport.protocol", "smtp");

			MailSSLSocketFactory sf = new MailSSLSocketFactory();

			sf.setTrustAllHosts(true);
			props.put("mail.smtp.ssl.enable", "true");
			props.put("mail.smtp.ssl.socketFactory", sf);

			Session session = Session.getInstance(props);

			Message msg = new MimeMessage(session);
			msg.setSubject(title);
			StringBuilder builder = new StringBuilder();
			builder.append(msgs);
			builder.append("\n");
			builder.append("\n时间 " + DateUtil.dateToStr(Calendar.getInstance().getTime(), DateUtil.DATETIME_FORMAT));
			msg.setText(builder.toString());
			msg.setFrom(new InternetAddress("ljm@91dida.cn"));

			Transport transport = session.getTransport();
			transport.connect("smtp.exmail.qq.com", "ljm@91dida.cn", "A55801680a");

			transport.sendMessage(msg, arg1);
			transport.close();
			return true;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			return false;
		}

	}

	public static void main(String[] args) throws MessagingException, GeneralSecurityException {
		Properties props = new Properties();

		// 开启debug调试
		props.setProperty("mail.debug", "false");
		// 发送服务器需要身份验证
		props.setProperty("mail.smtp.auth", "true");
		// 设置邮件服务器主机名
		props.setProperty("mail.host", "smtp.exmail.qq.com");

		props.setProperty("mail.smtp.port", "465");
		// 发送邮件协议名称
		props.setProperty("mail.transport.protocol", "smtp");

		MailSSLSocketFactory sf = new MailSSLSocketFactory();
		sf.setTrustAllHosts(true);
		props.put("mail.smtp.ssl.enable", "true");
		props.put("mail.smtp.ssl.socketFactory", sf);

		Session session = Session.getInstance(props);

		Message msg = new MimeMessage(session);
		msg.setSubject("ceshi");
		StringBuilder builder = new StringBuilder();
		builder.append("发送是否成功？");
		builder.append("\n");
		builder.append("\n时间 " + DateUtil.dateToStr(Calendar.getInstance().getTime(), DateUtil.DATETIME_FORMAT));
		msg.setText(builder.toString());
		msg.setFrom(new InternetAddress("ljm@91dida.cn"));

		Transport transport = session.getTransport();
		transport.connect("smtp.exmail.qq.com", "ljm@91dida.cn", "A55801680a");

		transport.sendMessage(msg,
				new Address[] { new InternetAddress("55801680@qq.com"), new InternetAddress("ljm@91dida.cn") });
		transport.close();
	}
}
